package cn.iocoder.yudao.module.lfd.controller.admin.schedule;

import cn.iocoder.yudao.module.lfd.dal.dataobject.schedule.NickScheduleDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.lfd.controller.admin.schedule.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.schedule.ScheduleDO;
import cn.iocoder.yudao.module.lfd.convert.schedule.ScheduleConvert;
import cn.iocoder.yudao.module.lfd.service.schedule.ScheduleService;

@Tag(name = "管理后台 - 排班")
@RestController
@RequestMapping("/lfd/schedule")
@Validated
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;

    @PostMapping("/create")
    @Operation(summary = "创建排班")
    @PreAuthorize("@ss.hasPermission('lfd:schedule:create')")
    public CommonResult<Long> createSchedule(@Valid @RequestBody ScheduleCreateReqVO createReqVO) {
        return success(scheduleService.createSchedule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新排班")
    @PreAuthorize("@ss.hasPermission('lfd:schedule:update')")
    public CommonResult<Boolean> updateSchedule(@Valid @RequestBody ScheduleUpdateReqVO updateReqVO) {
        scheduleService.updateSchedule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排班")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lfd:schedule:delete')")
    public CommonResult<Boolean> deleteSchedule(@RequestParam("id") Long id) {
        scheduleService.deleteSchedule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排班")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lfd:schedule:query')")
    public CommonResult<ScheduleRespVO> getSchedule(@RequestParam("id") Long id) {
        ScheduleDO schedule = scheduleService.getSchedule(id);
        return success(ScheduleConvert.INSTANCE.convert(schedule));
    }

    @GetMapping("/list")
    @Operation(summary = "获得排班列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('lfd:schedule:query')")
    public CommonResult<List<ScheduleRespVO>> getScheduleList(@RequestParam("ids") Collection<Long> ids) {
        List<ScheduleDO> list = scheduleService.getScheduleList(ids);
        return success(ScheduleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排班分页")
    @PreAuthorize("@ss.hasPermission('lfd:schedule:query')")
    public CommonResult<PageResult<NickScheduleDO>> getSchedulePage(@Valid SchedulePageReqVO pageVO) {
        PageResult<NickScheduleDO> pageResult = scheduleService.getSchedulePage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排班 Excel")
    @PreAuthorize("@ss.hasPermission('lfd:schedule:export')")
    @OperateLog(type = EXPORT)
    public void exportScheduleExcel(@Valid ScheduleExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ScheduleDO> list = scheduleService.getScheduleList(exportReqVO);
        // 导出 Excel
        List<ScheduleExcelVO> datas = ScheduleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "排班.xls", "数据", ScheduleExcelVO.class, datas);
    }

}
